ImageScroll = function(cfg) {
  var config = {
    body       : null,
    list       : null,
    speed      : 10, // 速度(毫秒)
    space      : 20, // 每次移动(px)
    page_width : 36 * 5, // 翻页宽度
    interval   : 7000, // 翻页间隔
    fill       : 0, // 整体移位
    move_lock  : false,
    move_time  : null,
    move_way   : "right",
    comp       : 0,
    auto_play  : null
  };
  $.extend(config, cfg);
  if (typeof(config.body) == 'string') {
    config.body = $('#' + config.body);
  }
  config.list = config.body.find('.scr_cont_list');
  // 设置上翻页按钮
  this.set_up_button = function(up_button) {
    if (typeof(up_button) == 'string') {
      up_button = $('#' + up_button);
    }
    up_button.on('mouseup', stop_up);
    up_button.on('mousedown', go_up);
    up_button.on('mouseout', stop_up);
  }
  // 设置下翻页按钮
  this.set_down_button = function(down_button) {
    if (typeof(down_button) == 'string') {
      down_button = $('#' + down_button);
    }
    down_button.on('mouseup', stop_down);
    down_button.on('mousedown', go_down);
    down_button.on('mouseout', stop_down);
  }
  this.init = function() {
    config.list.eq(1).html(config.list.eq(0).html());
    config.body.scrollLeft(config.fill >= 0 ? config.fill : config.list.eq(0).prop("scrollWidth") - Math.abs(config.fill));
    config.body.on('mouseover', function() {
      clearInterval(config.auto_play);
    });
    config.body.on('mouseout', auto_play);
    auto_play();
  }
  var auto_play = function() {
    clearInterval(config.auto_play);
    config.auto_play = setInterval(function() {
      go_down();
      stop_down();
    }, config.interval)
  }
  var go_up = function() {
    if (config.move_lock) {
      return;
    }
    clearInterval(config.auto_play);
    config.move_lock = true;
    config.move_way = "left";
    config.move_time = setInterval(scr_up, config.speed);
  }
  var stop_up = function() {
    if (config.move_way == "right") {
      return
    };
    clearInterval(config.move_time);
    if ((config.body.scrollLeft() - config.fill) % config.page_width != 0) {
      config.comp = config.fill - (config.body.scrollLeft() % config.page_width);
      comp_scr()
    } else {
      config.move_lock = false
    }
    auto_play();
  }
  var scr_up = function() {
    if (config.body.scrollLeft() <= 0) {
      config.body.scrollLeft(config.body.scrollLeft() + config.list.eq(0).outerWidth());
    }
    config.body.scrollLeft(config.body.scrollLeft() - config.space);
  }
  var go_down = function() {
    clearInterval(config.move_time);
    if (config.move_lock) {
      return;
    }
    clearInterval(config.auto_play);
    config.move_lock = true;
    config.move_way = "right";
    scr_down();
    config.move_time = setInterval(scr_down, config.speed);
  }
  var stop_down = function() {
    if (config.move_way == "left") {
      return
    };
    clearInterval(config.move_time);
    if (config.body.scrollLeft() % config.page_width - (config.fill >= 0 ? config.fill : config.fill + 1) != 0) {
      config.comp = config.page_width - config.body.scrollLeft() % config.page_width + config.fill;
      comp_scr()
    } else {
      config.move_lock = false
    }
    auto_play();
  }
  var scr_down = function() {
    if (config.body.scrollLeft() >= config.list.eq(0).prop("scrollWidth")) {
      config.body.scrollLeft(config.body.scrollLeft() - config.list.eq(0).prop("scrollWidth"));
    }
    config.body.scrollLeft(config.body.scrollLeft() + config.space);
  }
  var comp_scr = function() {
    if (config.comp == 0) {
      config.move_lock = false;
      return
    }
    var num,
      temp_speed = config.space,
      temp_space = config.space;
    if (Math.abs(config.comp) < config.page_width / 2) {
      temp_space = Math.round(Math.abs(config.comp / config.space));
      if (temp_space < 1) {
        temp_space = 1
      }
    }
    if (config.comp < 0) {
      if (config.comp < -temp_space) {
        config.comp += temp_space;
        num = temp_space
      } else {
        num = -config.comp;
        config.comp = 0
      }
      config.body.scrollLeft(config.body.scrollLeft() - num);
      setTimeout(comp_scr, temp_speed);
    } else {
      if (config.comp > temp_space) {
        config.comp -= temp_space;
        num = temp_space
      } else {
        num = config.comp;
        config.comp = 0
      }
      config.body.scrollLeft(config.body.scrollLeft() + num);
      setTimeout(comp_scr, temp_speed);
    }
  }
}
